import 'package:dio/dio.dart';
import 'package:flutter_demo/core/common/net/code.dart';
import 'package:flutter_demo/core/common/net/result_data.dart';
import 'package:flutter_demo/core/common/values/server.dart';
import 'package:get/instance_manager.dart';

class ResponseInterceptors extends InterceptorsWrapper {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    final RequestOptions option = response.requestOptions;
    ResultData value;
    try {
      if (response.statusCode != 200) {
        //http请求异常
        value = ResultData(response.data, Code.SYS_ERRO);
      } else if (response.data == null || response.data['code'] == null) {
        //未知异常
        value = ResultData(response.data, Code.SYS_ERRO, message: "系统崩溃了");
      } else {
        //正常情况
        value = ResultData(response.data['data'], response.data['code'],
            message: response.data['message']);
      }
    } catch (e) {
      logger.i(e.toString() + option.path);
      value = ResultData(response.data, response.statusCode!);
      rethrow;
    }
    response.data = value;
    handler.next(response);
    // super.onResponse(response, handler);
  }

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    logger.d("request path ${options.path}");
    super.onRequest(options, handler);
  }

  // @override
  // Future onResponse(Response response) {

  //   return Future.value(value);
  // }
}
